<template>
  <div class="contextmenu" :style="`left:${position.x}px;top:${position.y}px`" v-if="visible && list"
    @contextmenu.stop="() => void 0">
    <el-text line-clamp truncated="true" class="list_name">{{ list.title }}</el-text>
    <el-divider class="el-divide"></el-divider>
    <div :class="`contextmenu-item ${item.color || ''}`" @click.prevent="opens(item)" v-for="item in __list"
      :key="item.value">
      {{ item.label }}
    </div>
  </div>
</template>

<script>
import { defineComponent, onMounted, computed, watch, watchEffect, ref } from "vue";
import { is_object } from "../../tool/common.js";
export default defineComponent({
  name: "contextmenu",
  props: {
    list: {
      type: Object,
      defalut: () => null,
    },
    visible: {
      type: Boolean,
      default: () => false,
    },
  },
  setup(props, context) {
    function closeEmit() {
      context.emit("close");
    }
    function opens(e) {
      if (e && is_object(e)) {
        e.name = props.list.title;
        context.emit("command", e);
      }
      closeEmit();
    }
    onMounted(() => {
      window.addEventListener("click", () => {
        closeEmit();
      }, {
        capture: false
      });
      window.addEventListener("contextmenu", closeEmit);
      window.addEventListener("auxclick", closeEmit);
    });

    const __list = computed(() => props.list.list);
    let position = ref({
      x: 0,
      y: 0,
    });
    watchEffect(() => {
      if (props.list != null) {
        position.value = props.list.position;
      }
    });
    return {
      __list,
      position,
      opens,
    };
  },
});
</script>

<style scoped lang="scss">
.contextmenu {
  position: fixed;
  top: 0;
  box-shadow: -6px -5px 18px rgba(255, 255, 255, 0.1);
  left: 0;
  border: 1px solid #c0c4cc;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  width: 200px;
  height: auto;
  overflow: hidden;
  background-color: #ffffff;
  justify-content: start;
  border-radius: 5px;
  transform: translateY(-10px);
  transform-origin: 0 0;
  opacity: 0.4;
  align-items: left;
  animation-name: fade-in;
  animation-duration: 0.3s;
  animation-fill-mode: forwards;
  padding: 8px;
  box-sizing: border-box;

  ::v-deep(.list_name) {
    font-weight: 600;
    font-size: 17.5px;
  }

  .el-divide {
    margin-top: 8px;
    margin-bottom: 2.5px;
  }

  .contextmenu-item {
    cursor: pointer;
    background-color: transparent;
    border: none;
    padding: 6px;
    user-select: none;
    transition: all 0.12s;
    display: flex;
    position: relative;
    gap: 5px;
    border-radius: 4px;

    &:hover {
      background-color: #909399;
      color: #ebedf0;
    }

    &:active {
      transform: scale(0.98);
    }
  }
}

.red {
  color: rgb(218, 75, 75);
}

@keyframes fade-in {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
